package com.yikaiye.android.yikaiye.utils;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.ColorInt;

import org.jetbrains.annotations.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.reactivex.annotations.NonNull;

public class SpanContentParser {
    /**
     * 书名号的监听器
     */
    private BookCharListener bookCharListener = null;

    public SpanContentParser() {

    }

    // 书名号的监听器
    public void setBookCharListener(BookCharListener bookCharListener) {
        this.bookCharListener = bookCharListener;
    }

    /**
     * 解析用户协议《》中的内容(事件)
     */
    public SpannableString parseContent(String content) {
        Pattern AGREEMENT_PATTERN = Pattern.compile("《.*?》");
        SpannableString spannableString = new SpannableString(content);
        Matcher matcher = AGREEMENT_PATTERN.matcher(content);
        while (matcher.find()) {
            String agreementStr = matcher.group();
            spannableString.setSpan(new AgreementClickSpan(agreementStr),
                    matcher.start(), matcher.end(),
                    SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    // 用户协议span
    private class AgreementClickSpan extends ContentClickSpan {
        public AgreementClickSpan(String content) {
            super(content);
        }

        @Override
        void onClick(View widget, String content) {
            if (bookCharListener != null)
                bookCharListener.onClickBookChar(content);
        }
    }

    public abstract static class ContentClickSpan extends ClickableSpan {
        // 点击的内容
        String mContent;

        public ContentClickSpan(String content) {
            mContent = content;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#FFB226"));
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(@NotNull View widget) {
            onClick(widget, mContent);
        }

        abstract void onClick(View widget, String content);
    }

    public interface BookCharListener {
        void onClickBookChar(String name);
    }

    /**
     * 解析指定字符串 第一个
     *
     * @param str     整体语句
     * @param spanS   其中的特殊字句
     * @param colorId 指定颜色
     * @return SpannableString
     */
    public SpannableString parseSpecifiedSpan(@NonNull String str, @NonNull String spanS, @ColorInt int colorId) {
        SpannableString spannableString = new SpannableString(str);
        //第一个出现的位置
        final int start = str.indexOf(spanS);
        final int length = spanS.length();
        spannableString.setSpan(new ClickableSpan() {

            @Override
            public void onClick(@NotNull View widget) {
                if (bookCharListener != null)
                    bookCharListener.onClickBookChar(spanS);
            }

            @Override
            public void updateDrawState(@NotNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(colorId); //设置文字颜色
                // 去掉下划线
                ds.setUnderlineText(false);
            }

        }, start, start + length, 0);
        return spannableString;
    }
}
